<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParam" :inline="true">
        <!-- 查询日期 StartDate 和EndDate -->
        <el-form-item>
          <el-date-picker
            v-model="queryParam.month"
            type="month"
            placeholder="请选择月份"
            :disabled-date="disabledDate"
            style="width: 183px"
          />
        </el-form-item>
        <el-form-item prop="keywords">
          <el-input
            v-model="queryParam.keywords"
            placeholder="请按照关键字搜索"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery"> 搜索 </el-button>
          <el-button type="primary" :loading="download_loading" @click="handleExport"> 导出 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never">
      <template #header>
        <div v-if="currentSystem === 'Manager'" class="flex justify-between">
          <div>
            <el-button type="primary" @click="handleAdd()"> 发布 </el-button>
          </div>
        </div>
      </template>
      <el-table v-loading="tableLoading" :data="dataList" highlight-current-row border>
        <el-table-column label="序号" type="index" width="60" align="center"></el-table-column>
        <el-table-column key="title" label="通知标题" width="210" align="center" prop="title"> </el-table-column>
        <el-table-column
          key="content"
          label="通知内容"
          align="center"
          :show-overflow-tooltip="{ effect: 'light' }"
          min-width="160"
          prop="content"
        >
        </el-table-column>
        <el-table-column key="creator" label="发布人" align="center" width="80" prop="creator"> </el-table-column>
        <el-table-column key="createTime" label="发布日期" align="center" width="160" prop="createTime">
        </el-table-column>
        <el-table-column key="readedNum" label="查看次数" align="center" width="80" prop="readedNum"> </el-table-column>
        <el-table-column label="操作" fixed="right" width="220">
          <template #default="scope">
            <el-button
              v-if="currentSystem === 'Manager'"
              type="primary"
              link
              size="small"
              @click="handleEdit(scope.row.id)"
            >
              <i-ep-edit />
              重新发布
            </el-button>
            <el-button type="danger" link size="small" @click="handleDelete(scope.row.id)">
              <i-ep-grid />
              删除
            </el-button>
            <el-button type="primary" link size="small" @click="openDetail(scope.row)">
              <i-ep-grid />
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParam.pageIndex"
        v-model:limit="queryParam.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
    <add-form v-if="dialogAddOrEdit.visible" :dialog-add-or-edit="dialogAddOrEdit" @send-close="sendClose"></add-form>
    <detail v-if="dialogDetail.visible" :dialog-detail="dialogDetail" @send-close="sendClose"></detail>
  </div>
</template>
<script setup lang="ts">
// 引入文件
import { downloadFile } from "@/utils";
import addForm from "@/views/commonpage/notify/worker/addForm.vue";
import detail from "@/views/commonpage/notify/worker/detail.vue";
import { QueryParams } from "@/api/notify/types";
import { getWorkerNotifysByPage, delWorkerNotify, exportWorkerNotify } from "@/api/notify/index";
import moment from "moment";

// 声明变量
const queryParam = reactive<QueryParams>({});
const dataList = ref([]);
const total = ref(0);
const tableLoading = ref(false);
const currentSystem = ref(localStorage.getItem("usertypeName"));
const download_loading = ref(false);
const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
  title: "发布内容",
});
const dialogDetail = reactive<DialogOption>({
  visible: false,
  title: "通知详情",
});
//查询
const handleQuery = () => {
  tableLoading.value = true;
  if (queryParam.month) {
    queryParam.month = moment(queryParam.month).format("YYYY-MM-DD");
  }
  getWorkerNotifysByPage(queryParam).then((res) => {
    if (res.type === 0) {
      if (res.success) {
        dataList.value = res.data.list;
        total.value = res.data.total;
        queryParam.pageIndex = res.data.pageNum;
        queryParam.pageSize = res.data.pageSize;
      } else {
      }
    } else {
    }
    tableLoading.value = false;
  });
};

//导出
const handleExport = () => {
  download_loading.value = true;

  exportWorkerNotify(queryParam).then((res) => {
    downloadFile(res);
    download_loading.value = false;
  });
};

//添加工人通知
const handleAdd = () => {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.title = "发布内容";
  dialogAddOrEdit.id = 0;
};
//编辑工人通知
const handleEdit = (id: string) => {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.title = "编辑内容";
  dialogAddOrEdit.id = id;
};
//删除工人通知
const handleDelete = async (id: string) => {
  let confirm = await ElMessageBox.confirm("确认删除?", "删除提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => true)
    .catch(() => false);
  if (!confirm) {
    ElMessage({
      type: "info",
      message: "已取消！",
    });
    return;
  }
  delWorkerNotify(id).then((res) => {
    if (res.type === 0) {
      if (res.success) {
        ElMessage.success(res.msg);
        handleQuery();
      } else {
      }
    } else {
    }
  });
};
//打开详情
const openDetail = (row: any) => {
  dialogDetail.visible = true;
  dialogDetail.title = "查看详情";
  dialogDetail.id = row.id;
};

/**
 * 日期选择
 */
const disabledDate = (time: Date) => {
  return time.getTime() > Date.now();
};

/**关闭弹窗 */
function sendClose(dialog: string, query: boolean) {
  if (dialog == "add") {
    dialogAddOrEdit.visible = false;
  }
  if (dialog == "detail") {
    dialogDetail.visible = false;
  }
  if (query) {
    handleQuery();
  }
}
onMounted(() => {
  handleQuery();
});
</script>

<style scoped>
.app-container {
  position: relative;
  padding: 0 20px 0 20px;
  font-family: SourceHanSansCN;
}
:deep().el-form-item {
  margin-right: 15px;
}
</style>
